<template>
  <div>
    <el-button
        :key="item.key"
        :icon="item.icon"
        :type="item.type"
        @click="onButton(item)"
        v-for="item in buttons">{{ item.name }}
    </el-button>

  </div>
  <!--表单-->
  <el-dialog
      v-model="dialogVisible"
      :before-close="handleClose"
      title="Tips"
  >
    <commentForm
        :config=config
        :list=commentFormList
        source="user"
    ></commentForm>
  </el-dialog>
</template>

<script>
import {post} from '../request/api'
import config from '@/json/config.json'

export default {
  name: "CommButton",
  props: {
    //按钮集合
    buttons: {
      type: Array,
      default: () => [
        /*
         "key": "oqt_hs",
        "icon": "",
        "action": "add",
        "type": "primary",
        "exit": true,
        "name": "保存",
        "isCustom": false,
        "customPort": "",
        * */
      ] // 默认值是空数组
    },
  },
  data() {
    return {
      dialogVisible: false,
      commentFormList: [],
      config: {},
    }
  },
  inject: ["fResetForm", "parDisplay"],
  methods: {
    /*按钮触发触发方法*/
    onButton(item) {
      let action = item.action;
      console.log('action', action)
      if ('open' === action) {
        console.log('打开表单', item)
        //设置表单的字段
        this.commentFormList = config.userManageFormFieldList;
        //打开表单
        this.dialogVisible = true;
        //表单的配置
        this.config = config.userManageFormConfigList;
        console.log('打开表单', this.dialogVisible)
      } else if ('add' == action) {
        console.log('保存按钮', item)
        //参数处理
        let parMap = this.parDisplay()
        let serverUrl = "";
        serverUrl = "/user/addUser";
        console.log("add", parMap)
        post(serverUrl, parMap)
            .then(res => {
              console.log(res)
            })
            .catch(error => {
              console.log(error)
            })
      } else if ('reset' === action) {
        this.onResetForm()
      } else if ('close' === action) {
        console.log('关闭按钮', this.dialogVisible)
        // this.dialogVisible = true;
        console.log('关闭按钮', this.dialogVisible)
      }
    },
    onResetForm() {
      this.fResetForm();
    },
    handleClose() {
      console.log('关闭按钮', this.dialogVisible)
      this.dialogVisible = false;
    },
  },
}
</script>

<style scoped>

</style>
